package demo.app.nexd.com.demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.map.location.utils.Log;
import cn.nexd.map.rendering.SVGMapViewListener;
import cn.nexd.map.rendering.SVGMapViewOtherClickListener;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import cn.nexd.map.rendering.overlay.SVGMapLinesOverlay;
import cn.nexd.map.utils.SplineUtil;

/**
 * Created by lawrence on 2015/12/25.
 */
public class SearchCarActivity extends Activity {

    private SVGMapView svgMapView;

    private float[][] points = new float[][]{
            {36.283035f, 23.105272f}, {36.2222f, 79.43817f},
            {152.17307f, 81.87156f}, {153.2681f, 135.83191f},
            {169.69345f, 135.83191f}, {168.52417f, 150.39554f}, {163.23059f, 150.34534f}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_car_layout);
        svgMapView = (SVGMapView) findViewById(R.id.svgMapView);
        svgMapView.setMaxZoomValue(300);
        svgMapView.registerMapViewListener(new SVGMapViewListener() {
            @Override
            public void onMapLoadComplete(long floorId, String buildingName) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toDrawLine();
                    }
                });
            }

            @Override
            public void onMapLoadError() {

            }

            @Override
            public void onGetCurrentMap(Bitmap bitmap) {

            }
        });
        svgMapView.registerMapViewOtherClickListener(new SVGMapViewOtherClickListener() {
            @Override
            public void onClick(float screenX, float screenY, float mapX, float mapY) {
                Log.i("SearchCarActivity", "mapX = " + mapX + " \n mapY = " + mapY);
            }
        });
        svgMapView.loadMap(getAssets(), "101080551001.svg");
    }

    private float[] pre = null;

    private void toDrawLine() {

        MarkerOverlay start = new MarkerOverlay(this);
        start.setPositions(new PointF(points[0][0], points[0][1]));
        start.setRes(R.drawable.icon_st);
        svgMapView.addOverlay(start, 0);


        MarkerOverlay end = new MarkerOverlay(this);
        end.setPositions(new PointF(points[points.length - 1][0], points[points.length - 1][1]));
        end.setRes(R.drawable.icon_en);
        svgMapView.addOverlay(end, 0);

        svgMapView.refresh();

        SVGMapLinesOverlay linesOverlay = new SVGMapLinesOverlay(svgMapView);
        List<PointF> pointFs = new ArrayList<>();

        for (float[] point : points) {

            if (pre == null) {
                pre = new float[]{point[0], point[1]};
                continue;
            } else {
                List<Float[]> sPoint = SplineUtil.spline(pre, point, 1);
//                pointFs.clear();
                for (Float[] p : sPoint) {
                    pointFs.add(new PointF(p[0], p[1]));
                }
                pre = point;
                linesOverlay.setPositions(pointFs);
                svgMapView.addOverlay(linesOverlay, 1);
                svgMapView.refresh();
            }
        }
    }
}
